package com.dao.impl;


import com.dao.BaseDao;
import com.dao.StudentDao;
import com.pojo.Student;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zxl
 * @create :2022/9/28
 */
//dao的实现类
public class StudentDaoImpl extends BaseDao implements StudentDao {
    @Override
    public List<Student> selectByAll() {
        //如果数据库的字段名与实体的属性名不一致DBUtil无法针对名字进行封装数据
        //解决方案就是 起别名
        String sql = "select id,stu_name stuName,grade_name gradeName,age,address,ordered,statues from student";
        return selectForList(Student.class, sql);
    }

    @Override
    public int deleteStudent(Student student) {
        String sql = "delete from student where id = ?";
        return update(sql, student.getId());
    }

    @Override
    public int insertStudnet(Student student) {
        String sql = "insert into student values(?,?,?,?,?,?,?)";
        return update(sql, 0,
                student.getStuName(), student.getGradeName(),
                student.getAge(), student.getAddress(),
                student.getOrdered(), student.getStatues());
    }

    @Override
    public Student selectById(Student student) {
        String sql = "select id,stu_name stuName,grade_name gradeName,age,address,ordered,statues from student where id = ?";
        return selectForOne(Student.class, sql, student.getId());
    }

    @Override
    public int updateStudentById(Student student) {
        String sql = "update student set stu_name = ?,grade_name = ?,age = ?,address = ?,ordered = ?,statues = ? where id = ?";
        System.out.println(student.getStatues());
        return update(sql, student.getStuName(), student.getGradeName(),
                student.getAge(), student.getAddress(),
                student.getOrdered(), student.getStatues(), student.getId());
    }

    /**
     * @Override public List<Student> selectByPage(int begin, int size) {
     * String sql = "select id,stu_name stuName,grade_name gradeName,age,address,ordered,statues from student limit ?,?";
     * return selectForList(Student.class,sql,begin,size);  }
     */


    //带条件分页
    @Override
    public List<Student> selectByPage(int begin, int size, Student student) {
        String sql = "select id,stu_name stuName,grade_name gradeName,age,address,ordered,statues from student where 1 = 1 ";
        //集合存储参数
        List<Object> param = new ArrayList<>();

        if (student.getStatues() != null) {
            sql += "and statues = ? ";
            param.add(student.getStatues());
        }
        if (student.getStuName() != null && student.getStuName() != "") {
            sql += "and stu_name like ? ";
            String stuName = student.getStuName();
            param.add("%" +stuName + "%");
        }
        if (student.getGradeName() != null && student.getGradeName() != "") {
            sql += "and grade_name like ?";
            String gradeName = student.getGradeName();
            param.add("%" + gradeName + "%");
        }


        sql += " limit ?,?";
        System.out.println("sql=" + sql);
        //条件+limit begin pagSize
        Object[] params = new Object[param.size() + 2];
        //将集合中的每个参数获取出来 遍历没数组
        for (int i = 0; i < param.size(); i++) {
            params[i] = param.get(i);
        }

        System.out.println("size11 = " + size);
        System.out.println("begin11 = " + begin);
        //设置limit的两个参数
        params[params.length - 1] = size;
        params[params.length - 2] = begin;
        //参数不固定 不能直接传入 需要统计参数
        return selectForList(Student.class, sql, params);
    }

    @Override
    public int selectCount(Student student) {
        String sql = "select count(1) from student where 1 = 1 ";
        //集合存储参数
        List<Object> param = new ArrayList<>();


        if (student.getStatues() != null) {
            sql += "and statues = ? ";
            param.add(student.getStatues());
        }
        if (student.getStuName() != null && student.getStuName() != "") {
            sql += "and stu_name like ? ";
            String stuName = student.getStuName();
            param.add("%" +stuName + "%");
        }
        if (student.getGradeName() != null && student.getGradeName() != "") {
            sql += "and grade_name like ?";
            String gradeName = student.getGradeName();
            param.add("%" + gradeName + "%");
        }



        System.out.println(sql);

        Object[] params = new Object[param.size() ];
        //将集合中的每个参数获取出来 遍历没数组
        for (int i = 0; i < param.size(); i++) {
            params[i] = param.get(i);
        }
        //先转String    再转为int
        String s = selectForValue(sql,params).toString();
        return Integer.parseInt(s);
    }

}
